package com.yuju.util;

import java.io.IOException;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;

import com.yuju.init.fastdfs.FastDFSInit;
import com.yuju.model.upload.UploadFileModel;

/*import org.apache.commons.io.FilenameUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;

import com.yuju.upload.model.UploadFileModel;*/

public class FastDFSUtils {
	/*public static String uploadPic(UploadFileModel model) {
		String path = null;
		ClassPathResource resource = new ClassPathResource("resource/fdfs_client.conf");
		
		try {
			ClientGlobal.init(resource.getClassLoader().getResource("resource/fdfs_client.conf").getPath());
			
			TrackerClient trackerClient = new TrackerClient();
			TrackerServer trackerServer = trackerClient.getConnection();
			
			//StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
			StorageServer storageServer = null;
			StorageClient storageClient = new StorageClient(trackerServer, storageServer);
			
			// 文件扩展名
			String ext = FilenameUtils.getExtension(model.getName());
			// meta list是
			NameValuePair[] metaList = new NameValuePair[3];
			metaList[0] = new NameValuePair("fileName", model.getName());
			metaList[1] = new NameValuePair("fileExt", ext);
			metaList[2] = new NameValuePair("fileSize", String.valueOf(model.getSize()));
			
			//path = storageClient1.upload_file1(model.getPic(), ext, metaList);
			String[] uploadResults = storageClient.upload_file(model.getPic(), ext, metaList);
			if (uploadResults != null) {
				path = uploadResults[1];
				System.out.println(uploadResults[0]);
				System.out.println(uploadResults[1]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return path;
	}*/
	
	/**
	 * 
	 * 功能：上传文件<br/>
	 * @author xuehj
	 * 
	 * @param model 文件对象信息
	 * @Date 2018年1月5日 上午10:56:32 <br/>
	 */
	public static String uploadPic(UploadFileModel model) {
		String path = null;
		// 文件扩展名
		String ext = FilenameUtils.getExtension(model.getName());
		// meta list是
		NameValuePair[] metaList = new NameValuePair[3];
		metaList[0] = new NameValuePair("fileName", model.getName());
		metaList[1] = new NameValuePair("fileExt", ext);
		metaList[2] = new NameValuePair("fileSize", String.valueOf(model.getSize()));
		try {
			path = FastDFSInit.getStorageClient1().upload_file1(model.getPic(), ext, metaList);
		} catch (IOException | MyException e) {
			e.printStackTrace();
		}
		return path;
	}
	
	public static int delFile(String path) {
		try {
			String group = null;
			if (Utils.isNotNull(path)) {
				group = path.substring(0, path.indexOf("/"));
				path = path.substring(path.indexOf("/") + 1, path.length());
			}
			return FastDFSInit.getStorageClient().delete_file(group, path);
		} catch (IOException | MyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
